# -*- coding: utf-8 -*-
#pylint: disable-msg=E1101
'''
Created on 09/ott/2009

@author: sax
'''

from testapp.tests.basetest import ViewTestCase
from testapp.models import Author, Book
from xadmin.ajax.service import AjaxService


services = [ AjaxService(Author.objects.all(), ['id','__unicode__'], ['last_name__istartswith']),
             AjaxService(Author.objects.all(), ['id','__unicode__'], ['last_name__istartswith', 'first_name__istartswith']),
             AjaxService(Author.objects.all(), ['id','__unicode__'], ['last_name__istartswith'], 
                                                                    {"first_name__istartswith":"a"}),

             AjaxService(Book.objects.all(), ['id','__unicode__'], ['author__last_name__istartswith']),
             AjaxService(Book.objects.all(), ['id','__unicode__','author.__unicode__'], ['author__last_name__istartswith']),

             AjaxService(Author.objects.all(), ['id','__unicode__'], ['first_name','wp_url']),


            ]

class ServiceTestCase(ViewTestCase):
    urls = 'testapp.tests.urls_services'
    
    def test_base(self):
        ret =  self.client.get("/service/0?q=a" )
        self.assertEqual(ret.content,"1|Alighieri Dante\n")        
        
    def test_multiple_filter(self):
        ret =  self.client.get("/service/1?q=a" )
        self.assertEqual(ret.content,"1|Alighieri Dante\n3|Manzoni Alessandro\n")        

    def test_forced_filter(self):
        ret =  self.client.get("/service/2?q=m" )
        self.assertEqual(ret.content,"3|Manzoni Alessandro\n")        

    def test_filter_on_fk(self):
        ret =  self.client.get("/service/3?q=t" )
        self.assertEqual(ret.content,"20|Gerusalemme liberata\n21|Re Torrismondo\n")   

    def test_filter_and_returns_on_fk(self):
        ret =  self.client.get("/service/4?q=t" )
        self.assertEqual(ret.content,"20|Gerusalemme liberata|Tasso Torquato\n21|Re Torrismondo|Tasso Torquato\n")

    def test_limit(self):
        ret =  self.client.get("/service/4?q=t&limit=1" )
        self.assertEqual(ret.content,"20|Gerusalemme liberata|Tasso Torquato\n")
        
    def test_invalid_limit(self):
        self.assertRaises(ValueError, self.client.get, "/service/4?q=t&limit=-1")

    def test_syntax_any(self):        
        ret =  self.client.get("/service/4?q=__any__" )
        l =  ret.content.split("\n")
        self.assertEqual(l[0],'1|Divina Commedia|Alighieri Dante')
        self.assertEqual(l[9],'10|Adelchi|Manzoni Alessandro')

    def test_syntax_null(self):        
        ret =  self.client.get("/service/5?q=none" )
    
    def test_full(self):
        ret =  self.client.get("/service/4?q=__any__&limit=10&timestamp=1255524542991" )
        l =  ret.content.split("\n")
        self.assertEqual(l[0],'1|Divina Commedia|Alighieri Dante')
        self.assertEqual(l[9],'10|Adelchi|Manzoni Alessandro')
                
class T(object):
    def get_urls(self):
        from django.conf.urls.defaults import patterns, url
        ret = []
        for i,s in enumerate(services):
            ret += patterns('', url(r'^%s$' % i, s) )
        return ret
        
    urls = property(get_urls)
    
t = T()